Imports System
Imports System.Xml

'Converted

Public Class AccountsMsg
	Inherits BaseMsg

	'<CPCAPI v="1.1">
	'      <AccountReq ReqID="testID" ProductID="LN"/>
	'</CPCAPI>
	'<CPCAPI v="1.1">
	'      <AccountRsp ReqID="testID" ProductID="LN">
	'            <PtyGrp>
	'                  <Pty ID="PIONEERACCT" R="24"/>
	'                  <Pty ID="ewt" R="1">
	'                        <Sub ID="Pioneer Futures, Inc." Typ="5"/>
	'                  </Pty>
	'                  <Pty ID="444" R="4">
	'                        <Sub ID="Test Clearing Firm" Typ="5"/>
	'                  </Pty>
	'            </PtyGrp>
	'            <PtyGrp>
	'                  <Pty ID="BARCLAYSACCT" R="24"/>
	'                  <Pty ID="barclayscapital" R="1">
	'                        <Sub ID="Barclays Capital Inc." Typ="5"/>
	'                  </Pty>
	'                  <Pty ID="444" R="4">
	'                        <Sub ID="Test Clearing Firm" Typ="5"/>
	'                  </Pty>
	'            </PtyGrp>

	Public Sub New()
        RequestType = "PtyEntlmntReq" '"AccountReq"
        ResponseType = "PtyEntlmntRpt" '"AccountRsp"
	End Sub

	Protected Overrides Function RequestMsg(ByRef XmlW As XmlTextWriter) As Boolean

		If m_prodid = "" Then
			Return False
		End If

        XmlW.WriteStartElement("ReqPty")
		XmlW.WriteAttributeString("ID", Globals.CompanyID)
        XmlW.WriteAttributeString("R", "30") '- 30 = broker firm
        XmlW.WriteEndElement()

        XmlW.WriteStartElement("ReqR")
        XmlW.WriteAttributeString("R", "24") '- 24 = account 
        XmlW.WriteEndElement()

        XmlW.WriteStartElement("InstrmtScope")
        XmlW.WriteAttributeString("Oper", "1")
        XmlW.WriteAttributeString("ID", m_prodid)
        XmlW.WriteAttributeString("Src", "H")
        XmlW.WriteAttributeString("Exch", "NYMEX")

        XmlW.WriteAttributeString("SecTyp", SecTyp)
        XmlW.WriteEndElement()

		Return True
	End Function

	Protected Overrides Sub ResponseParse(ByRef node As XmlNode)

        Dim pty As XmlNode
        Dim dtl As XmlNode
        'Dim attr As XmlAttribute

		Dim dbi As dbInterface = Globals.dbi

        'If node.Attributes("ProductID").InnerText = m_prodid Then

        dbi.nonquery("delete from nymx_prod_accounts where prod_id=" & dbi.quote(m_prodid))

        For Each pty In node.ChildNodes
            If pty.Name = "PtyEntlmnt" Then
                For Each dtl In pty.ChildNodes
                    If dtl.Name = "PtyDetl" Then
                        InsertParty(dtl)
                    End If
                Next
            End If

        Next
        'For Each ptygrp In node.ChildNodes
        'InsertParty(ptygrp)
        'Next
        'End If

	End Sub

	Private Sub InsertParty(ByRef ptygrp As XmlNode)
		'            <PtyGrp>
		'                  <Pty ID="BARCLAYSACCT" R="24"/>
		'                  <Pty ID="barclayscapital" R="1">
		'                        <Sub ID="Barclays Capital Inc." Typ="5"/>
		'                  </Pty>
		'                  <Pty ID="444" R="4">
		'                        <Sub ID="Test Clearing Firm" Typ="5"/>
		'                  </Pty>
		'            </PtyGrp>

		Dim dbi As dbInterface = Globals.dbi
		Dim party As XmlNode
		Dim ptysub As XmlNode

		Dim acctid As String = ""
		Dim execid As String = ""
		Dim execnm As String = ""
		Dim clearid As String = ""
		Dim clearnm As String = ""
		Dim brokerid As String = ""
		Dim brokernm As String = ""

		Dim r As String
		Dim nm As String

        If ptygrp.Name = "PtyDetl" Then
            acctid = ptygrp.Attributes("ID").InnerText

            For Each party In ptygrp.ChildNodes
                If party.Name <> "ReltdPtyDetl" Then
                    Continue For
                End If

                r = party.Attributes("R").InnerText
                nm = ""

                If party.ChildNodes.Count > 0 Then ' will never have child nodes at this level
                    For Each ptysub In party.ChildNodes
                        If ptysub.Name = "Sub" Then
                            nm = ptysub.Attributes("ID").InnerText
                        End If
                    Next
                End If

                Select Case r
                    ' Case "24"
                    '    acctid = party.Attributes("ID").InnerText

                    Case "7" ' was 1
                        execid = party.Attributes("ID").InnerText
                        execnm = nm

                    Case "1" ' was 4
                        clearid = party.Attributes("ID").InnerText
                        clearnm = nm

                    Case "30" ' was 7
                        brokerid = party.Attributes("ID").InnerText
                        brokernm = nm

                End Select

            Next

            dbi.SetupStoredProc("sp_nymx_ins_product_account")
            dbi.AddStoredProcParam("@prod_id", SqlDbType.VarChar, m_prodid)
            dbi.AddStoredProcParam("@acct_id", SqlDbType.VarChar, acctid)
            dbi.AddStoredProcParam("@exec_firm_id", SqlDbType.VarChar, execid)
            dbi.AddStoredProcParam("@exec_firm_name", SqlDbType.VarChar, execnm)
            dbi.AddStoredProcParam("@clear_firm_id", SqlDbType.VarChar, clearid)
            dbi.AddStoredProcParam("@clear_firm_name", SqlDbType.VarChar, clearnm)
            dbi.AddStoredProcParam("@broker_firm_id", SqlDbType.VarChar, brokerid)
            dbi.AddStoredProcParam("@broker_firm_name", SqlDbType.VarChar, brokernm)
            dbi.RunStoredProc()

        End If

	End Sub

End Class

